import mongoose, { Document, Schema } from 'mongoose'

export interface IGiftTransaction extends Document {
  senderId: mongoose.Types.ObjectId
  receiverId: mongoose.Types.ObjectId
  roomId: mongoose.Types.ObjectId
  giftId: mongoose.Types.ObjectId
  amount: number
  totalPrice: number
  createdAt: Date
}

const GiftTransactionSchema = new Schema({
  senderId: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true,
  },
  receiverId: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true,
  },
  roomId: {
    type: Schema.Types.ObjectId,
    ref: 'Room',
    required: true,
  },
  giftId: {
    type: Schema.Types.ObjectId,
    ref: 'Gift',
    required: true,
  },
  amount: {
    type: Number,
    required: true,
    min: 1,
  },
  totalPrice: {
    type: Number,
    required: true,
  },
  createdAt: {
    type: Date,
    default: Date.now,
  },
})

// 创建索引
GiftTransactionSchema.index({ roomId: 1, createdAt: -1 })
GiftTransactionSchema.index({ senderId: 1, createdAt: -1 })
GiftTransactionSchema.index({ receiverId: 1, createdAt: -1 })

export const GiftTransaction = mongoose.model<IGiftTransaction>(
  'GiftTransaction',
  GiftTransactionSchema
)
